﻿using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using AutoUpdaterDotNET;
using DangKyDLL;
using DevComponents.DotNetBar;
using HGP_QLKH002.HGP_QLKH002.BUS;
using System.Drawing;
namespace HGP_QLKH002
{
    public partial class frmMain : Office2007Form
    {
        public frmMain()
        {
            Thread t = new Thread(new ThreadStart(splashScreen));
            t.Start();
            Thread.Sleep(1000);
            t.Abort();
            InitializeComponent();
            this.EnableGlass = false;
            MessageBoxEx.EnableGlass = false;
        }

        private QLKHBUS bus;
        MaHoa mh=new MaHoa();
        private void frmMain_Load_1(object sender, EventArgs e)
        {
            mh.KtraLic();
            CheckUpdate();

            bus = new QLKHBUS();
            StatusBar();
            LoadCombo();
            Loadinfo();
            glue_tenkh.Properties.PopupFormSize = new Size(180, 300);
            glue_madh.Properties.PopupFormSize = new Size(180, 300);
        }

        private void btn_dh_Click(object sender, EventArgs e)
        {
            GetData_Ma();
        }

        private void btn_kh_Click(object sender, EventArgs e)
        {
            GetData_Ten();
        }

        #region Hàm Viết Thêm
        private void ResetLabel()
        {
            txt_madh.Text = "N/A";
            lbl_tenkh.Text = "N/A";
            lbl_diachi.Text = "N/A";
            lbl_khach.Text = "N/A";
            lbl_cty.Text = "N/A";
            lbl_phone.Text = "N/A";
            lbl_email.Text = "N/A";
            lbl_fb.Text = "N/A";
            lbl_ngaygiao.Text = "N/A";
            lbl_ngaylap.Text = "N/A";
            lbl_tongiten.Text = "N/A";
            lbl_datcoc.Text = "N/A";
            lbl_conlai.Text = "N/A";
            lbl_duyet1.Text = "N/A";
            lbl_duyet2.Text = "N/A";
            lbl_duyet3.Text = "N/A";
        }

        private void LoadCombo()
        {
            if (InternetConnection.IsConnectedToInternet())
            {
                glue_madh.Properties.DataSource = bus.GetorderID();
                glue_madh.Properties.DisplayMember = "meta_value";
                glue_madh.Properties.ValueMember = "post_id";

                glue_tenkh.Properties.DataSource = bus.Gettenkh();
                glue_tenkh.Properties.DisplayMember = "meta_value";
                glue_tenkh.Properties.ValueMember = "post_id";

            }
            else
            {
                MessageBox.Show("Không kết nối được CSDL. Vui lòng thử lại sau!");
                return;
            } 
        }

        private void GetData_Ma()
        {

            var rowHandle = gridView1.FocusedRowHandle;
            if (InternetConnection.IsConnectedToInternet())
            {
            int id = Convert.ToInt16(gridView1.GetRowCellValue(rowHandle, "post_id"));
            DataTable dt_id = bus.GetTenByMa("orderID", id);
            DataTable dt_tenkh = bus.GetTenByMa("ten_khach_hang", id);
            DataTable dt_dc = bus.GetTenByMa("dia_chi", id);
            DataTable dt_phone = bus.GetTenByMa("dien_thoai", id);
            DataTable dt_kh = bus.GetTenByMa("dai_dien_khach_hang", id);
            DataTable dt_cty = bus.GetTenByMa("dai_dien_cong_ty", id);
            DataTable dt_email = bus.GetTenByMa("email", id);
            DataTable dt_fb = bus.GetTenByMa("facebook", id);
            DataTable dt_ngaygiao = bus.GetTenByMa("thoi_gian_nhan_mau", id);
            DataTable dt_ngaylap = bus.GetTenByMa("ngay_xac_lap_hop_dong", id);
            DataTable dt_tongtien = bus.GetTenByMa("thanh_tien", id);
            DataTable dt_datcoc = bus.GetTenByMa("dat_coc", id);
            DataTable dt_conlai = bus.GetTenByMa("con_lai", id);
            DataTable dt_duy1 = bus.GetTenByMa("confirm_1", id);
            DataTable dt_duy2 = bus.GetTenByMa("confirm_2", id);
            DataTable dt_duy3 = bus.GetTenByMa("confirm_3", id);

            ResetLabel();

            // Mã KH
            if (dt_id.Rows.Count != 0)
                txt_madh.Text = dt_id.Rows[0]["meta_value"].ToString();
            else
                txt_madh.Text = "Not Found";
            // Tên KH
            if (dt_tenkh.Rows.Count != 0)
                lbl_tenkh.Text = dt_tenkh.Rows[0]["meta_value"].ToString();
            else
                lbl_tenkh.Text = "Not Found";
            // Địa chỉ KH
            if (dt_dc.Rows.Count != 0)
                lbl_diachi.Text = dt_dc.Rows[0]["meta_value"].ToString();
            else
                lbl_diachi.Text = "Not Found";
            // Điện thoại KH
            if (dt_phone.Rows.Count != 0)
                lbl_phone.Text = dt_phone.Rows[0]["meta_value"].ToString();
            else
                lbl_phone.Text = "Not Found";
            // Đại diện KH
            if (dt_kh.Rows.Count != 0)
                lbl_khach.Text = dt_kh.Rows[0]["meta_value"].ToString();
            else
                lbl_khach.Text = "Not Found";
            // Đại diện Cty
            if (dt_cty.Rows.Count != 0)
                lbl_cty.Text = dt_cty.Rows[0]["meta_value"].ToString();
            else
                lbl_cty.Text = "Not Found";
            // Email Khách
            if (dt_email.Rows.Count != 0)
                lbl_email.Text = dt_email.Rows[0]["meta_value"].ToString();
            else
                lbl_email.Text = "Not Found";
            // FB Khách
            if (dt_fb.Rows.Count != 0)
                lbl_fb.Text = dt_fb.Rows[0]["meta_value"].ToString();
            else
                lbl_fb.Text = "Not Found";

            // Ngày Nhận Mẫu
            if (dt_ngaygiao.Rows.Count != 0)
                lbl_ngaygiao.Text = dt_ngaygiao.Rows[0]["meta_value"].ToString();
            else
                lbl_ngaygiao.Text = "Not Found";
            // Ngày Lập ĐH
            if (dt_ngaylap.Rows.Count != 0)
                lbl_ngaylap.Text = dt_ngaylap.Rows[0]["meta_value"].ToString();
            else
                lbl_ngaylap.Text = "Not Found";
            // Thành tiền
            if (dt_tongtien.Rows.Count != 0)
                lbl_tongiten.Text = dt_tongtien.Rows[0]["meta_value"].ToString();
            else
                lbl_tongiten.Text = "Not Found";
            // Đặt cọc
            if (dt_datcoc.Rows.Count != 0)
                lbl_datcoc.Text = dt_datcoc.Rows[0]["meta_value"].ToString();
            else
                lbl_datcoc.Text = "Not Found";
            // Còn lại
            if (dt_conlai.Rows.Count != 0)
                lbl_conlai.Text = dt_conlai.Rows[0]["meta_value"].ToString();
            else
                lbl_conlai.Text = "Not Found";
            // Duyệt 1
            if (dt_duy1.Rows.Count != 0)
                lbl_duyet1.Text = dt_duy1.Rows[0]["meta_value"].ToString();
            else
                lbl_duyet1.Text = "Not Found";
            // Duyệt 2
            if (dt_duy2.Rows.Count != 0)
                lbl_duyet2.Text = dt_duy2.Rows[0]["meta_value"].ToString();
            else
                lbl_duyet2.Text = "Not Found";
            // Duyệt 3
            if (dt_duy3.Rows.Count != 0)
                lbl_duyet3.Text = dt_duy3.Rows[0]["meta_value"].ToString();
            else
                lbl_duyet3.Text = "Not Found";
            }
            else
            {
                MessageBox.Show("Không kết nối được CSDL. Vui lòng thử lại sau!");
                return;
            }
        }

        private void GetData_Ten()
        {
            var rowHandle = gridLookUpEdit1View.FocusedRowHandle;
            if (InternetConnection.IsConnectedToInternet())
            {
            int id = Convert.ToInt16(gridLookUpEdit1View.GetRowCellValue(rowHandle, "post_id"));
            DataTable dt_id = bus.GetTenByMa("orderID", id);
            DataTable dt_tenkh = bus.GetTenByMa("ten_khach_hang", id);
            DataTable dt_dc = bus.GetTenByMa("dia_chi", id);
            DataTable dt_phone = bus.GetTenByMa("dien_thoai", id);
            DataTable dt_kh = bus.GetTenByMa("dai_dien_khach_hang", id);
            DataTable dt_cty = bus.GetTenByMa("dai_dien_cong_ty", id);
            DataTable dt_email = bus.GetTenByMa("email", id);
            DataTable dt_fb = bus.GetTenByMa("facebook", id);
            DataTable dt_ngaygiao = bus.GetTenByMa("thoi_gian_nhan_mau", id);
            DataTable dt_ngaylap = bus.GetTenByMa("ngay_xac_lap_hop_dong", id);
            DataTable dt_tongtien = bus.GetTenByMa("thanh_tien", id);
            DataTable dt_datcoc = bus.GetTenByMa("dat_coc", id);
            DataTable dt_conlai = bus.GetTenByMa("con_lai", id);
            DataTable dt_duy1 = bus.GetTenByMa("confirm_1", id);
            DataTable dt_duy2 = bus.GetTenByMa("confirm_2", id);
            DataTable dt_duy3 = bus.GetTenByMa("confirm_3", id);

            ResetLabel();
            // Mã KH
            if (dt_id.Rows.Count != 0)
                txt_madh.Text = dt_id.Rows[0]["meta_value"].ToString();
            else
                txt_madh.Text = "Not Found";
            // Tên KH
            if (dt_tenkh.Rows.Count != 0)
                lbl_tenkh.Text = dt_tenkh.Rows[0]["meta_value"].ToString();
            else
                lbl_tenkh.Text = "Not Found";
            // Địa chỉ KH
            if (dt_dc.Rows.Count != 0)
                lbl_diachi.Text = dt_dc.Rows[0]["meta_value"].ToString();
            else
                lbl_diachi.Text = "Not Found";
            // Điện thoại KH
            if (dt_phone.Rows.Count != 0)
                lbl_phone.Text = dt_phone.Rows[0]["meta_value"].ToString();
            else
                lbl_phone.Text = "Not Found";
            // Đại diện KH
            if (dt_kh.Rows.Count != 0)
                lbl_khach.Text = dt_kh.Rows[0]["meta_value"].ToString();
            else
                lbl_khach.Text = "Not Found";
            // Đại diện Cty
            if (dt_cty.Rows.Count != 0)
                lbl_cty.Text = dt_cty.Rows[0]["meta_value"].ToString();
            else
                lbl_cty.Text = "Not Found";
            // Email Khách
            if (dt_email.Rows.Count != 0)
                lbl_email.Text = dt_email.Rows[0]["meta_value"].ToString();
            else
                lbl_email.Text = "Not Found";
            // FB Khách
            if (dt_fb.Rows.Count != 0)
                lbl_fb.Text = dt_fb.Rows[0]["meta_value"].ToString();
            else
                lbl_fb.Text = "Not Found";

            // Ngày Nhận Mẫu
            if (dt_ngaygiao.Rows.Count != 0)
                lbl_ngaygiao.Text = dt_ngaygiao.Rows[0]["meta_value"].ToString();
            else
                lbl_ngaygiao.Text = "Not Found";
            // Ngày Lập ĐH
            if (dt_ngaylap.Rows.Count != 0)
                lbl_ngaylap.Text = dt_ngaylap.Rows[0]["meta_value"].ToString();
            else
                lbl_ngaylap.Text = "Not Found";
            // Thành tiền
            if (dt_tongtien.Rows.Count != 0)
                lbl_tongiten.Text = dt_tongtien.Rows[0]["meta_value"].ToString();
            else
                lbl_tongiten.Text = "Not Found";
            // Đặt cọc
            if (dt_datcoc.Rows.Count != 0)
                lbl_datcoc.Text = dt_datcoc.Rows[0]["meta_value"].ToString();
            else
                lbl_datcoc.Text = "Not Found";
            // Còn lại
            if (dt_conlai.Rows.Count != 0)
                lbl_conlai.Text = dt_conlai.Rows[0]["meta_value"].ToString();
            else
                lbl_conlai.Text = "Not Found";
            // Duyệt 1
            if (dt_duy1.Rows.Count != 0)
                lbl_duyet1.Text = dt_duy1.Rows[0]["meta_value"].ToString();
            else
                lbl_duyet1.Text = "Not Found";
            // Duyệt 2
            if (dt_duy2.Rows.Count != 0)
                lbl_duyet2.Text = dt_duy2.Rows[0]["meta_value"].ToString();
            else
                lbl_duyet2.Text = "Not Found";
            // Duyệt 3
            if (dt_duy3.Rows.Count != 0)
                lbl_duyet3.Text = dt_duy3.Rows[0]["meta_value"].ToString();
            else
                lbl_duyet3.Text = "Not Found";
            }
            else
            {
                MessageBox.Show("Không kết nối được CSDL. Vui lòng thử lại sau!");
                return;
            }
        }

        private void LoadWeb()
        {
            try
            {
                wb_xemmau.ScriptErrorsSuppressed = true;
                wb_xemmau.Navigate("http://xemmau.hogiaphat.com.vn");
                while (wb_xemmau.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi kết nối website!\nLỗi: " + ex.Message);
            }
        }

        #region Status Bar
        private void StatusBar()
        {
            lbl_ngay.Text = "Hôm nay: " + DateTime.Today.Day.ToString() + "/" + DateTime.Today.Month.ToString() +
                            "/" + DateTime.Today.Year.ToString() + " ! Chúc 1 ngày tốt lành.";
        }

        #endregion

        private void CheckUpdate()
        {
            AutoUpdater.Start("https://dl.dropboxusercontent.com/u/79154193/Update_mrdat/HGP_QLKH/hgp_qlkh.xml");
        }

        private void splashScreen()
        {
            Application.Run(new frmsplash());
        }

        private void Loadinfo()
        {
            string f = "License.lic";
            if (File.Exists(f) == true)
            {
                string line = File.ReadLines(f).Skip(2).Take(1).First();
                lbl_dangky.Text = line;
            }
        }
        #endregion



        private void btn_xem1_Click(object sender, EventArgs e)
        {
            try
            {
                wb_xemmau.ScriptErrorsSuppressed = true;
                wb_xemmau.Navigate(lbl_duyet1.Text);
                while (wb_xemmau.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi kết nối website!\nLỗi: " + ex.Message);
            }
        }

        private void btn_xem2_Click(object sender, EventArgs e)
        {
            try
            {
                wb_xemmau.ScriptErrorsSuppressed = true;
                wb_xemmau.Navigate(lbl_duyet2.Text);
                while (wb_xemmau.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi kết nối website!\nLỗi: " + ex.Message);
            }
        }

        private void btn_xem3_Click(object sender, EventArgs e)
        {
            try
            {
                wb_xemmau.ScriptErrorsSuppressed = true;
                wb_xemmau.Navigate(lbl_duyet3.Text);
                while (wb_xemmau.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi kết nối website!\nLỗi: " + ex.Message);
            }
        }

        private void btn_chitiet_Click(object sender, EventArgs e)
        {
            LoadWeb();
            wb_xemmau.Document.GetElementById("orderID").SetAttribute("value", txt_madh.Text);
            wb_xemmau.Document.GetElementById("submit_product_order_search").InvokeMember("click");
        }

        private void btn_Help_Click(object sender, EventArgs e)
        {
            frmHelp frm=new frmHelp();
            frm.Show();
        }
    }
}
